document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
var myScroll;
$().ready(function(){
	if(window.parent==window.self){
		addhistroy()
	}else{
		if($(window.parent.document).find('#poper')){
			var name=$(window.parent.document).find('#poper').find('.popnamebox').text()
			if($('#header').actual('height')!=0  && $('#header').actual('height')){
				$(window.parent.document).find('#poper').find('.popnamebox').hide().css({'top':0})
				$(window.parent.document).find('#poper').find('.popcontent').css({'top':0})
				$('#header').show().find('.headname').text(name).addClass('popcancle')
				var top=$('#header').height()
			}else{
				var top=0
			}
			$('#contenter').css({'top':top})
			//$('#contenter').css({'top':0})
		}
	}

	var height=$(window).height()
	var width=$(window).width()

	if(window.parent!=window.self){
		var bottom=0
	}else{
		if($('#header').length==1){
			$('#header').show()
			var top=$('#header').height()
		}else{
			var top=0
		}
		if($('#bottom').length==1){
			$('#bottom').show()
			var bottom=$('#bottom').height()
		}else{
			var bottom=0
		}
	}
	$('#contenter').css({'top':top,'bottom':bottom}).show()
	//$('body').append('<img src="static/img/top.png" width="80" id="scrolltop">')
	$('#scrolltop').css({'bottom':bottom+20})

	$('#header .backico').add('#header .cancle').add('#header .popcancle').add('#back').on("tap", function (event) {
		if(window.parent==window.self){
			backhistroy()
		}else{
			removepoper($(this))
		}
		event.preventDefault();
	})

	$('body').on("tap",'[data_url]', function (event) {
		var data_url=$(this).attr('data_url')
		if(data_url!='' && data_url!=undefined){
			window.location=data_url
		}
		event.stopImmediatePropagation();
		event.preventDefault();
	})

	$('#bottom .col-xs-4').on("tap", function (event) {
		$('#bottom .col-xs-4').removeClass('act')
		$(this).addClass('act')
		event.preventDefault();
	})

	if($('#tap').length==1){
		$('#contenter').html('<div class="loading center"><img src="static/img/load.png"></div>')
		tab($('#tap'))
	}else{
		myScroll=new IScroll('#contenter',{
			scrollbars: true,//有滚动条
			mouseWheel: true,//允许滑轮滚动
			fadeScrollbars: true,//滚动时显示滚动条，默认隐藏，并且是淡出淡入效果
			bounce:true,//边界反弹
			interactiveScrollbars:true,//滚动条可以拖动
			shrinkScrollbars:'scale',// 当滚动边界之外的滚动条是由少量的收缩。'clip' or 'scale'.
			momentum:true,// 允许有惯性滑动
			click:false,
			tap:true,
			preventDefault: false,
			disablePointer: true
		})
	}
	$(document).touchend(function(e){
		if($(':focus').get(0)!=event.target){
			$(':focus').blur()
		}
	})
	// $('body').on('focusin','input',function(){
	// 	if($(this).parents('#contenter').length==1){
	// 		//myScroll.scrollToElement($(this).get(0))
	// 	}
	// })
	// $('body').on('focusin','textarea',function(){
	// 	if($(this).parents('#contenter').length==1){
	// 		//myScroll.scrollToElement($(this).get(0))
	// 	}
	// })
})

var t=new Array()
function bannerimg(imgbox,imgwidth,imgheight,nummode,norun){
	$(imgbox).css({position:'relative',overflow:'hidden',display:'none'})
	$(imgbox).children().wrapAll('<div></div>')
	$(imgbox).children().css({position:'absolute',left:0,top:0})
	var height=$(window).height()
	var width=$(window).width()
	imgheight=width/imgwidth*imgheight
	var imglist=$(imgbox).children().children()
	imglist.css({display:'block',float:'left'})
	var start=imglist.eq(0).clone()
	var last=imglist.eq(-1).clone()
	$(imgbox).children().prepend(last).append(start)
	$(imgbox).children().width((imglist.length+2)+'00%')
	if(nummode==1){
		var bannernum='<div class="bannernum">'
		for(i=0;i<imglist.length;i++){
			bannernum+='<div class="circle'
			if(i==0){
				bannernum+=' act '
			}
			if(i==imglist.length-1){
				bannernum+=' last '
			}
			bannernum+='"></div>'
		}
		bannernum+='</div>'
		$(imgbox).append(bannernum)
	}else if(nummode==2){
		var bannernum='<div class="row bannernum2">'
		for(i=0;i<imglist.length;i++){
			bannernum+='<div class="circle col-xs-4'
			if(i==0){
				bannernum+=' act '
			}
			bannernum+='"></div>'
		}
		bannernum+='</div>'
		$(imgbox).parent().append(bannernum)
	}
	$(imgbox+' img').attr('width',width).attr('height',imgheight)
	$(imgbox).css('height',imgheight)
	$(imgbox).show()
	$(imgbox+' img').lazyload({
		container : $(imgbox).children(),
		effect      : "fadeIn"
	})

	var simgScrollstart=true
	var simgScroll = new IScroll(imgbox,{
		probeType: 3,
		scrollX: true,
		scrollY: false,
		snap:true,
		startX:-width,
		momentum:false,
		bounce:false,
		bounceTime:200,
		tap:true,
		disablePointer: true
	});
	myScroll.refresh();
	clearTimeout(t[imgbox])
	if(imglist.length>1 && !norun){
		simgScrollrun()
	}
	function simgScrollrun(){
		t[imgbox]=setTimeout(function(){
			var page=simgScroll.currentPage.pageX+1
			if(page==0){
				page=2
			}
			simgScroll.goToPage(page,0,500)
			simgScrollrun()
		},3000)
	}
	simgScroll.on('scroll', function(){
		if(this.directionX!=0 && myScroll){
			myScroll.disable()
		}
		if(simgScrollstart){
			simgScroll.goToPage(1,0,-1)
		}
		var currentPage=this.currentPage.pageX
		if(this.currentPage.pageX==0){
			currentPage=this.pages.length-2
		}else if(this.currentPage.pageX>this.pages.length-2){
			currentPage-=this.pages.length-2
		}
		if(nummode==1){
			$(imgbox).find('.bannernum .circle').removeClass('act').eq(currentPage-1).addClass('act')
		}else if(nummode==2){
			$(imgbox).parent().find('.bannernum2 .circle').removeClass('act').eq(currentPage-1).addClass('act')
		}
	})
	simgScroll.on('scrollEnd', function(){
		if(simgScrollstart==true){
			simgScrollstart=false
		}else{
			if(!myScroll.enabled){
				myScroll.enable()
			}
		}
		if(this.currentPage.pageX==0){
			simgScroll.goToPage(this.pages.length-2,0,0)
		}else if(this.currentPage.pageX==this.pages.length-1){
			simgScroll.goToPage(1,0,0)
		}
	})
}
var ajaxcall=new Array();
function ajax(ac,set,method,callback,ajaxcallac){
	if(!ajaxcallac){
		ajaxcallac=ac
	}
	if(ajaxcall[ajaxcallac]){
		ajaxcall[ajaxcallac].abort();
	}
	var tempajax=$.ajax({
		url:'getcontent.php?ac='+ac,
		data:set,
		type:method,
		dataType:'json',
		success:function(data){
			if(data.error){
				if(data.error=='login'){
					bindbox()
				}else{
					alerterror(data.error)
				}
			}else{
				eval('callback(data)')
			}
		}
	})
	ajaxcall[ajaxcallac]=tempajax
}

function bindbox(){
	bgcover('hide')
	$('#logins').remove()
	bgcover('show',0,hidebindbox)
	$('body').append('<div id="logins" class="center" style="display:block"><b>身份验证</b><div id="close"><i></i></div><ul class="clf"><li><div class="l">预留手机号</div><div class="r"><input type="txt" name="phone" maxlength=11 value="" placeholder="请输入预留手机号码" class="phone"></div></li><li><div class="l">验证码</div><div class="r"><input type="txt" class="code" name="code" maxlength=6 value="" placeholder="请输入验证码" style="width:185px"> <button type="button" name="button" class="r send">获取验证码</button></div></li><li><div class="l">身份证</div><div class="r"><input type="txt" maxlength=6 name="cardno" class="cardno" value="" placeholder="请输入身份证后六位"></div></li></ul><a href="javascript:void(0)" class="bind">确定</a></div>')
	$('#close').on("tap", function (event) {
		bgcover('hide')
		$('#logins').remove()
		event.preventDefault();
	})

	$('#logins .send').on("tap", function (event) {
		if($(this).hasClass('send')){
			var patt=/^(13\d{2}|14[57]\d|15[^4,\D]\d|17[678]\d|18\d{2}|170[059])\d{7}$/
			var mobile=$('#logins .phone').val()
			if(!patt.test(mobile)){
				alerterror('请输入正确的手机号')
			}else{
				$(this).removeClass('send')
				var set=({'mobile':mobile,type:1})
				$(this).text('60s...')
				timerun($(this),60)
				ajax("captcha",set,'POST',function(data){
					alerterror('短信已发送，请注意查收')
				})
			}
		}
	})

	$('#logins .bind').on("tap", function (event) {
		var patt=/^(13\d{2}|14[57]\d|15[^4,\D]\d|17[678]\d|18\d{2}|170[059])\d{7}$/
		var mobile=$('#logins .phone').val()
		if(!patt.test(mobile)){
			alerterror('请输入正确的手机号')
		}else{
			var captcha=$('#logins .code').val()
			if(captcha.length!=6){
				alerterror('请输入正确的手机验证码')
			}else{
				var idcard6number=$('#logins .cardno').val()
				if(idcard6number.length!=6){
					alerterror('请输入身份证后六位')
				}else{
					var set=({'mobile':mobile,'captcha':captcha,'idcard6number':idcard6number})
					ajax("bindWechatAccount",set,'POST',function(data){
						alerterror('登陆成功');
						setTimeout(function(){
							window.location.reload()
						},3000)
					})
				}
			}
		}
	})

	$('#logins').keydown(function(e){
		var curKey = e.which;
		if(curKey == 13){
			var patt=/^(13\d{2}|14[57]\d|15[^4,\D]\d|17[678]\d|18\d{2}|170[059])\d{7}$/
			var mobile=$('#logins .phone').val()
			if(!patt.test(mobile)){
				alerterror('请输入正确的手机号')
			}else{
				var captcha=$('#logins .code').val()
				if(captcha.length!=6){
					alerterror('请输入正确的手机验证码')
				}else{
					var idcard6number=$('#logins .cardno').val()
					if(idcard6number.length!=6){
						alerterror('请输入身份证后六位')
					}else{
						var set=({'mobile':mobile,'captcha':captcha,'idcard6number':idcard6number})
						ajax("bindWechatAccount",set,'POST',function(data){
							alerterror('登陆成功');
							setTimeout(function(){
								window.location.reload()
							},3000)
						})
					}
				}
			}
		}
	})
}
function timerun(obj,sec){
	var s = setInterval(function(){
		sec--;
		obj.text(sec+'s...');
		if(sec<=0){
			obj.addClass('send').text('获取验证码')
			clearInterval(s)
		}
	}, 1000);
}

function hidebindbox(){
	$('#logins').remove()
	event.stopImmediatePropagation();
}

function alerterror(content){
	if($('#alert').length!=0){
		$('#alert').remove()
	}
	$('body').append('<div id="alert"><div>'+content+'</div></div>')
	clearTimeout(t)
	var t=setTimeout(function(){
			$('#alert').fadeOut(2000,function(){
			$(this).remove()
		})
	},1000)
}

var listint

function listin(obj,y,i){
	obj.css({'transform':'translate(0px, 0px) translateZ(0px)'})
	$('#contenter .row').css('opacity',1)
	//myScroll.refresh();
	return false

	clearTimeout(listint)
	var step1=25
	var step2=35
	var step3=40
	if(obj.height()<10 && i){
		i=step3
	}
	if(!i){
		i=0
	}
	if(i<=step1){
		start=y
		end=0
		allstep=step1
		step=i
	}else if(i<=step2){
		start=0
		end=y/10
		allstep=step2-step1
		step=i-step1
	}else{
		start=y/10
		end=0
		allstep=step3-step2
		step=i-step2
	}
	listint=setTimeout(function(){
		obj.css({'transform':'translate(0px, '+checky(start,end,allstep,step)+'px) translateZ(0px)'})
		if(i==0){
			$('#contenter .row').css('opacity',1)
			myScroll.refresh();
		}
		if(i!=step3){
			listin(obj,y,i+1)
		}else{
			$('#pullUp').add('.pullUp').show()
		}
	},20)
}

function checky(start,end,allstep,step){
	var y=start-(start-end)*(step/allstep)
	return parseInt(y);
}

var ajaxcall=new Array();
var pullDownEl, pullDownL;
var pullUpEl, pullUpL;
var loadingStep = 0;//加载状态0默认，1显示加载状态,2,达到既定加载位置3执行加载数据，只有当为0时才能再次加载，这是防止过快拉动刷新
var topisshow=0
function iscrolldetail(){
	if(myScroll){
		myScroll.destroy()
	}
	myScroll = new IScroll('#contenter',{
		probeType: 3,//probeType：1对性能没有影响。在滚动事件被触发时，滚动轴是不是忙着做它的东西。probeType：2总执行滚动，除了势头，反弹过程中的事件。这类似于原生的onscroll事件。probeType：3发出的滚动事件与到的像素精度。注意，滚动被迫requestAnimationFrame（即：useTransition：假）。
		scrollbars: true,//有滚动条
		mouseWheel: true,//允许滑轮滚动
		fadeScrollbars: true,//滚动时显示滚动条，默认影藏，并且是淡出淡入效果
		bounce:true,//边界反弹
		interactiveScrollbars:true,//滚动条可以拖动
		shrinkScrollbars:'scale',// 当滚动边界之外的滚动条是由少量的收缩。'clip' or 'scale'.
		momentum:true,// 允许有惯性滑动
		click:false,
		tap:true,
		deceleration:0.005,
		preventDefault: false,
		disablePointer: true
	})
	var bottom=0
	if($('#bottom').length!=0){
		bottom=$('#bottom').height()
	}
	//滚动时
	myScroll.on('scroll', function(){
		pullDownEl = $('#pullDown');
		pullDownL = pullDownEl.find('.pullDownLabel');
		pullDownI = pullDownEl.find('.pullDownIcon');

		pullUpEl = $('#pullUp');
		pullUpL = pullUpEl.find('.pullUpLabel');
		if(loadingStep == 0){
			if (this.y > 5) {
				//下拉刷新效果
				pullDownL.html('下拉即可刷新');
				pullDownEl.show()
				myScroll.refresh();
				loadingStep = 1;
			}else if (this.y < (this.maxScrollY + 5) && this.y<0){
				//上拉刷新效果
				if($('#pullUp').length!=0){
					pullUpEl.show();
					pullUpL.html('努力加载中...');
					myScroll.refresh();
					loadingStep = 3;
				}
			}
		}
		if(loadingStep == 1){
			var vy=53
			if(topisshow==1){
				vy-=53
			}
			if (this.y > vy) {
				//下拉刷新效果
				pullDownI.removeClass('rotatearrow_2').addClass('rotatearrow_1')
				pullDownL.html('松开即可刷新');
				pullDownEl.css('margin-top',0)
				myScroll.refresh();
				topisshow=1
				loadingStep = 2;
			}
		}
		if(loadingStep == 2){
			var vy=53
			if(topisshow==1){
				vy-=53
			}
			if (this.y < vy){
				pullDownL.html('下拉即可刷新');
				pullDownI.removeClass('rotatearrow_1').addClass('rotatearrow_2')
				loadingStep = 1;
			}
		}
		if(this.directionY==-1 && this.y<-200){
			$('#scrolltop').stop(1,1).fadeTo(500,1)
		}else{
			$('#scrolltop').stop(1,1).fadeTo(500,0)
		}
	});
	//滚动完毕
	myScroll.on('scrollEnd',function(){
		pullDownEl = $('#pullDown');
		pullDownL = pullDownEl.find('.pullDownLabel');
		pullDownI = pullDownEl.find('.pullDownIcon');

		pullUpEl = $('#pullUp');
		pullUpL = pullUpEl.find('.pullUpLabel');
		setTimeout(function(){
			$('#contenter').resize()
		},100)
		if(loadingStep == 1){
			pullDownEl.css('margin-top',-53)
			myScroll.refresh();
			myScroll.scrollTo(0,0)
			topisshow=1
			loadingStep = 0;
		}
		if(loadingStep == 2){
			pullDownI.html('<div class="loading"><img width=20 src="static/img/load.png"></div>').css({'padding':"0 0 6px 0"})
			pullUpL.html('努力加载中...');
			loadingStep = 4;
			pullDownAction();
		}
		if(loadingStep==3){
			loadingStep = 4;
			pullUpAction();
		}
		/*if(loadingStep == 1){
			if (pullUpEl.attr('class').match('flip|loading')) {
				pullUpEl.removeClass('flip').addClass('loading');
				pullUpL.html('Loading1...');
				loadingStep = 3;
				pullUpAction();
			}else if(pullDownEl.attr('class').match('flip|loading')){
				pullDownEl.removeClass('flip').addClass('loading');
				pullDownL.html('Loading2...');
				loadingStep = 3;
				pullDownAction();
			}
		}  */
	})
}
function GetDateDiff(startDate,endDate){
    var startTime =Date.parse(startDate.slice(0,10))
    var endTime = Date.parse(endDate.slice(0,10))
	var dates = parseInt((startTime-endTime)/(1000*60*60*24));
    return dates;
}

function tab(obj){
	var index=obj.index('.tabcolumn')
	//updatehistroy(index)
	var i=obj.attr('data_type')
	var order_para=obj.attr('order_para')
	$('.tabname .tabcolumn').removeClass('act')
	obj.addClass('act')
	obj.siblings().find('.orderarrow').hide()
	if(order_para!=undefined && order_para!=''){
		order_para=JSON.parse(order_para)
		var data_para=JSON.parse(obj.attr('data_para'))
		var temp
		for(x in order_para){
			temp=order_para[x]
			order_para[x]=data_para[x]
			data_para[x]=temp
		}
		obj.attr('order_para',JSON.stringify(order_para))
		obj.attr('data_para',JSON.stringify(data_para))
		var arrowtype=$.trim(obj.find('.orderarrow').html())
		if(arrowtype=='↓'){
			arrowtype=' ↑'
		}else{
			arrowtype=' ↓'
		}
		obj.find('.orderarrow').html(arrowtype).show()
	}
	/*if(bg==1){
		$('#contenter').css('background','#FFFFFF')
	}else if(bg==2){
		$('#contenter').css('background','#f7f7f7')
	}else{
		$('#contenter').css('background','#EEEEEE')
	}*/
	if(obj.attr('data_set')!='' && obj.attr('data_set')!=undefined){
		if(obj.attr('data_mode')!=undefined){
			var mode=obj.attr('data_mode')
		}else{
			var mode=0
		}
		getdetail(obj,mode)
	}else{
		i=obj.index()
		$('#contenter .contenterbox').hide().eq(i).show()
		new IScroll('#contenter',{tap:true,disablePointer: true})
	}
}


function creatcontrol(){
	$('body').append('<object id="UnionControl" type="application/x-vrent" width="200" height="24"><param name="onload" value="pluginLoaded" /><param name="onRetRSAdata" value="onRetRSAdata" /></object>')
}

function pksm(pksm2,param){
	if($('#UnionControl').length==0){
		creatcontrol('UnionControl');
	}
	param=$('#UnionControl').get(0).UnionSmEnvelope(param,pksm2,'')
	return param;
}

function getdetail(obj,mode,contentbox){
	var i=obj.attr('data_type')
	var url=obj.attr('data_set')
	var para=obj.attr('data_para')
	if(mode==undefined){
		mode=0
	}
	if(mode==0){
		if(!contentbox){
			$('#contenter').html('<div class="loading center"><img src="static/img/load.png"></div>')
		}else{
			$(contentbox).html('<div class="loading" style="margin-top:100px;text-align:center"><img src="static/img/load.png"></div>')
		}
	}
	var set=''
	if(para!=undefined && para!=''){
		para=JSON.parse(para)
		if(mode!=2 && para['from']!=undefined){
			para['from']=0
		}
		u=0
		for(x in para){
			if(u!=0){
				set+='&'
			}
			set+=x+'='+para[x]
			u++
		}
	}
	// 下拉即可刷新（下） 松开即可刷新（上） 努力加载中（逆时针转圈 ）   更多商品上架中 敬请期待~
	var width=$(window).width()
	ajax(url,para,'GET',function(data){
		if(mode!=2){
			var contenttop='<div id="pullDown"><div class="pullDownIcon"><div class="glyphicon glyphicon-arrow-down"></div></div><div class="pullDownLabel">下拉即可刷新</div></div>'
		}else{
			var contenttop=''
		}
		var contentlist=getdetaildata(data,para['from'])
		var count=data.count
		var from=para['from']+para['to']
		para['from']=from
		obj.attr('data_para',JSON.stringify(para))
		var doremove=0
		if(from<count){
			if(mode!=2){
				var pullup='<div class="pullUpIcon"><div class="loading" style="padding:0 0 6px 0"><img width=20 src="static/img/load.png"></div></div><div class="pullUpLabel">上拉显示更多...</div>'
				var contenttbottom='<div id="pullUp">'+pullup+'</div>'
			}else{
				var contenttbottom=''
			}
		}else{
			var pullup='<div style="height:40px;font-size:20px;width:250px;margin:13px auto;text-align:center">没有更多了~</div>'
			var contenttbottom='<div class="col-xs-12 pullUp"'
			if(mode!=2){
				contenttbottom+='style="display:none"'
			}
			contenttbottom+='>'+pullup+'</div>'
			doremove=1
		}
		if(mode==2){
			pullUpEl.hide()
			if(!contentbox){
				contentlist=contenttop+contentlist+contenttbottom
				pullUpEl.before(contentlist)
				if(doremove==1){
					pullUpEl.remove()
				}
			}else{
				$(contentbox+' div').eq(0).append(contentlist)
				if(doremove==1){
					pullUpEl.replaceWith(contenttbottom);
				}
			}
			$(".notlazyload img").lazyload({
				container : $('#contenter'),
				effect      : "fadeIn"
			})
			$('.notlazyload').removeClass('notlazyload')
			myScroll.refresh();
		}else{
			if(!contentbox){
				contentbox='#contenter'
				contenttop='<div class="row" style="opacity:0">'+contenttop
				contenttbottom+='</div>'
				contentlist=contenttop+contentlist+contenttbottom
			}else{
				if(mode==0){
					$('#pullDown').remove()
					$('#contenter div').eq(0).prepend(contenttop)
					$('#pullUp').add('.pullUp').remove()
					$('#contenter div').eq(0).append(contenttbottom)
				}else{
					loadingStep=0
					topisshow=0
					$("#pullDown").removeAttr('style').html('<div class="pullDownIcon"><div class="glyphicon glyphicon-arrow-down"></div></div><div class="pullDownLabel">下拉即可刷新</div></div>')
					$('#pullUp').add('.pullUp').replaceWith(contenttbottom);
				}
				contentlist='<div class="row" style="opacity:0">'+contentlist+'</div>'
			}
			$(contentbox).html(contentlist)
			listin($(contentbox),$(contentbox).height())
			setTimeout(function(){
				$(".notlazyload img").lazyload({
					container : $(contentbox),
					effect      : "fadeIn"
				})
				setTimeout(function(){
					$('.notlazyload').removeClass('notlazyload')
				},100)
			},500)
			setTimeout(function(){
				$(contentbox).resize()
			},1000)
			iscrolldetail(contentbox)
			$('#scrolltop').on("tap", function (event) {
				myScroll.scrollTo(0,0,500)
				event.preventDefault();
			})
		}
		loadingStep = 0;
		getdetailend(mode)
	})
}

function poper(pop_name,pop_url){
	if($('#poper').length!=0){
		return false
	}
	$('body').append('<div id="poper"><div class="popnamebox container-fluid"><div class="row"><div class="col-xs-12"><div class="backico"></div><div class="popname">'+pop_name+'</div></div></div></div><div class="popcontent"></div></div>')
	/*if (window.self!= window.parent) {
		window.parent.$('#poper .popnamebox').hide()
		window.parent.$('#poper .popcontent').css({'top':0})
	}*/
	var top=$('#poper .popnamebox').height()
	$('#poper .popcontent').css({'top':top})
	$('#poper').animate({'left':0},400,function(){
		$('#poper .popcontent').html('<iframe width="100%" height="100%" border=0 src='+pop_url+'></iframe>')
	})
	$('#poper .backico').on("tap", function (event){
		removepoper($(this))
		event.preventDefault();
	})
}

function removepoper(that,callback){
	var parentobj
	var obj
	if(that.parents('#poper').length==1){
		obj=$('#poper')
		/*if(window.parent!=window.self){
			parentobj=$(window.parent.document).find('#poper')
		}*/
	}else if(window.parent!=window.self){
		obj=$(window.parent.document).find('#poper')
		/*if(window.top!=window.parent){
			parentobj=$(window.parent.parent.document).find('#poper')
		}*/
	}
	if(obj){
		obj.animate({'left':'100%'},400,function(){
			/*if (parentobj){
				parentobj.find('.popnamebox').show()
				parentobj.find('.popcontent').css({'top':parentobj.find('.popnamebox').height()})
			}*/
			if(callback){
				callback()
			}
			obj.remove()
		})
	}
}

function bgcover(mode,isload,callback){
	if(mode=='show'){
		if($('#bgcover').length==0){
			var cover='<div id="bgcover">'
			if(isload==1){
				cover+='<div class="loading center"><img src="static/img/load.png"></div>'
			}
			cover+='</div>'
			$('body').append(cover)
		}
		if(isload==0){
			$('#bgcover').on("tap", function (event) {
				if($('#bgcover div').length!=1){
					bgcover('hide')
				}
				if(callback){
					callback()
				}
				event.preventDefault();
			})
		}
		$('#bgcover').show()
	}else{
		if($('#bgcover').length!=0){
			$('#bgcover').remove()
		}
	}
}
function addhistroy(href){
	var length=sessionStorage.length
	if(!href){
		var href=window.location.href
	}
	if(sessionStorage[length-1]!=href){
		sessionStorage.setItem(length,href)
	}
}

function clearhistroy(){
	for(i in sessionStorage){
		sessionStorage.removeItem(i)
	}
}

function backhistroy(){
	var length=sessionStorage.length
	if(length>1){
		var url=sessionStorage.getItem(length-2)
		sessionStorage.removeItem(length-1)
	}else{
		var url='/index.php'
	}
	window.location=url
}

function updatehistroy(mode){
	var length=sessionStorage.length
	var href=window.location.href+'#'+mode
	sessionStorage.setItem(length-1,href)
}

function popdetail(popcontent){
	bgcover('show',0,hidepopdetail)
	$('body').append(popcontent)
	$('.popdetailclose').on("tap", function (event) {
		bgcover('hide')
		$('.popdetail').remove()
		event.preventDefault();
	})
}

function hidepopdetail(){
	$('.popdetail').remove()
}

function popselect(attr,value){
	var height=$(window).height()
	var maxheight=parseInt(height*0.7)
	attr=JSON.parse(attr)
	var popdetail='<div class="popselectbox" style="max-height:'+maxheight+'px"><div class="popselectrelativebox" style="position:relative"><div class="popselect"><div class="popselectscroll">'
	for(i in attr){
		popdetail+='<div class="popselectoption'
		if(value==attr[i]){
			popdetail+=' act '
		}
		popdetail+='" data_key="'+i+'">'+attr[i]+'</div>'
	}
	popdetail+='</div></div><div class="canclepopselectbox"><div class="canclepopselect">取消</div></div></div></div>'
	bgcover('show',0,hidepopselect)
	$('body').append(popdetail)
	var setheight=$('.canclepopselectbox').height()
	var popheight=$('.popselect').height()
	if(popheight+setheight<maxheight){
		maxheight=popheight+setheight
	}
	$('.popselectrelativebox').height(maxheight)
	$('.popselect').height(maxheight-setheight)
	$('.popselectbox').css({'display':'none','opacity':1}).slideDown(function(){
		new IScroll('.popselect',{tap:true,scrollbars: true,disablePointer: true})
	})
	$('.canclepopselect').on("tap", function (event) {
		bgcover('hide')
		$('.popselectbox').slideUp(function(){
			$(this).remove()
		})
		event.preventDefault();
	})
}

function hidepopselect(){
	$('.popselectbox').slideUp(function(){
		$(this).remove()
	})
}

var popscrolllist=new Array()
var simgScrollstart=new Array()
function popdate(obj){
	var height=$(window).height()
	var value=obj.val()
	if(value!=''){
		var dt=new Date(Date.parse(value))
	}else{
		var dt=new Date()
	}
	var year=dt.getFullYear()
	var month=dt.getMonth()+1
	var day=dt.getDate()
	var width=1/3*100;
	var popdetail='<div class="popdatebox"><div class="getpopdatebox"><div class="cancelpopdate">取消</div><div class="getpopdate">完成</div><div style="clear:both"></div></div><div class="popdaterelativebox" style="position:relative"><div class="popdate" style="right:'+width*2+'%"><div class="popdatescroll">'
	popdetail+='<div class="popdateoption"></div>'
	for(i=1900;i<=2100;i++){
		popdetail+='<div class="popdateoption'
		if(year==i){
			popdetail+=' act '
		}
		popdetail+='" data_key="'+i+'">'+i+'</div>'
	}
	popdetail+='<div class="popdateoption"></div>'
	popdetail+='</div></div><div class="popdate" style="left:'+width+'%;right:'+width+'%"><div class="popdatescroll">'
	popdetail+='<div class="popdateoption"></div>'
	for(i=1;i<=12;i++){
		popdetail+='<div class="popdateoption'
		if(month==i){
			popdetail+=' act '
		}
		popdetail+='" data_key="'+i+'">'+i+'</div>'
	}
	popdetail+='<div class="popdateoption"></div>'
	popdetail+='</div></div><div class="popdate" style="left:'+width*2+'%"><div class="popdatescroll">'
	popdetail+='<div class="popdateoption"></div>'
	var days=getdays(year,month)
	for(i=1;i<=days;i++){
		popdetail+='<div class="popdateoption'
		if(day==i){
			popdetail+=' act '
		}
		popdetail+='" data_key="'+i+'">'+i+'</div>'
	}
	popdetail+='<div class="popdateoption"></div>'
	popdetail+='</div></div></div></div>'
	bgcover('show',0,hidepopdate)
	$('body').append(popdetail)
	var popdateoption=$('.popdateoption').height()
	$('.popdaterelativebox').append('<div style="height:2px;width:100%;background:#DCDCDC;position:absolute;top:'+(popdateoption-1)+'px"></div>')
	$('.popdaterelativebox').append('<div style="height:2px;width:100%;background:#DCDCDC;position:absolute;top:'+(popdateoption-1)*2+'px"></div>')
	$('.popdatebox').css({'display':'none','opacity':1}).slideDown(function(){
		$('.popdate').each(function(i){
			var index=$('.act',this).index()
			var set={tap:true,snap:'.popdateoption',probeType:2,vScroll:true,checkDOMChanges:true}
			if(index!=-1){
				set['startY']=-popdateoption*(index-1)
				set['disablePointer']=true
			}
			var popscroll=new IScroll(this,set)
			popdatescrollinit(popscroll,i,index)
		})
	})
	$('.getpopdate').on("tap", function (event) {
		obj.val(year+'-'+month+'-'+day)
		bgcover('hide')
		$('.popdatebox').slideUp(function(){
			$(this).remove()
		})
		event.preventDefault();
	})

	$('.cancelpopdate').on("tap", function (event) {
		bgcover('hide')
		$('.popdatebox').slideUp(function(){
			$(this).remove()
		})
		event.preventDefault();
	})

	function popdatescrollinit(popscroll,i,index){
		popscrolllist[i]=popscroll
		simgScrollstart[i]=true
		popscroll.on('scroll', function(){
			if(simgScrollstart[i]==true){
				popscroll.goToPage(0,index-1,-1)
				simgScrollstart[i]=false
			}
		})
		popscroll.on('scrollEnd',function(){
			$('.popdate').eq(i).find('.popdateoption').removeClass('act').eq(this.currentPage.pageY+1).addClass('act')
			var changeday=false
			var value=$('.popdate').eq(i).find('.popdateoption.act').text()
			if(i==0){
				if($('.popdate').eq(0).find('.popdateoption.act').text()!=year){
					year=value
					changeday=true
				}
			}else if(i==1){
				if($('.popdate').eq(1).find('.popdateoption.act').text()!=month){
					month=value
					changeday=true
				}
			}else{
				day=value
			}
			if(changeday==true){
				var obj=$('.popdate').eq(2)
				var index=obj.find('.popdateoption').index(obj.find('.act'))
				getdays(year,month)
				var dayhtml=''
				dayhtml+='<div class="popdateoption"></div>'
				var days=getdays(year,month)
				if(index>days){
					index=days
				}
				for(n=1;n<=days;n++){
					dayhtml+='<div class="popdateoption'
					if(index==n){
						dayhtml+=' act '
					}
					dayhtml+='" data_key="'+n+'">'+n+'</div>'
				}
				dayhtml+='<div class="popdateoption"></div>'
				$('.popdatescroll').eq(2).html(dayhtml)
				//popscrolllist[2].refresh()
				popscrolllist[2].destroy()
				var set={tap:true,snap:'.popdateoption',probeType:2}
				if(index!=-1){
					set['startY']=-popdateoption*(index-1)
					set['disablePointer']=true
				}
				popscrolllist[2]=new IScroll(obj.get(0),set)
				popdatescrollinit(popscrolllist[2],2,index)
			}
		});
	}
}

function hidepopdate(){
	$('.popdatebox').slideUp(function(){
		$(this).remove()
	})
}

function getdays(year,month){
	var days= 30
	if(month == 2){
		if(year % 4==0){
			if(year % 100==0 && year % 400!=0){
				days=28
			}else{
				days=29
			}
		}else{
			days=28
		}
	}
	else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
		days= 31;
	}
	else{
		days= 30;
	}
	return days
}

function popcity(callback,provinceobj,cityobj,areaobj){
	var height=$(window).height()
	var num=1
	if(provinceobj!=undefined){
		var provincename=provinceobj.val()
	}
	if(cityobj!=undefined){
		var cityname=cityobj.val()
		num++
	}
	if(areaobj!=undefined){
		var areaname=areaobj.val()
		num++
	}
	var width=1/num*100;
	var popdetail='<div class="popcitybox"><div class="getpopcitybox"><div class="cancelpopcity">取消</div><div class="getpopcity">完成</div><div style="clear:both"></div></div><div class="popcityrelativebox" style="position:relative">'
	popdetail+='<div class="popcity" style="right:'+width*(num-1)+'%"><div class="popcityscroll"><div class="popcityoption"></div>'
	for(i in area){
		popdetail+='<div class="popcityoption'
		if((provincename!='' && provincename==area[i]['name']) || (provincename=='' && i==0)){
			provincename=area[i]['name']
			var province=i
			popdetail+=' act '
		}
		popdetail+='" data_key="'+i+'">'+area[i]['name']+'</div>'
	}
	popdetail+='<div class="popcityoption"></div>'
	popdetail+='</div></div>'
	if(cityobj!=undefined){
		popdetail+='<div class="popcity" style="left:'+width+'%;right:'+width*(num-2)+'%"><div class="popcityscroll">'
		popdetail+='<div class="popcityoption"></div>'
		for(i in area[province]['state']){
			popdetail+='<div class="popcityoption'
			if((cityname!='' && cityname==area[province]['state'][i]['name']) || (cityname=='' && i==0)){
				cityname=area[province]['state'][i]['name']
				var city=i
				popdetail+=' act '
			}
			popdetail+='" data_key="'+i+'">'+area[province]['state'][i]['name']+'</div>'
		}
		popdetail+='<div class="popcityoption"></div>'
		popdetail+='</div></div>'
	}
	if(areaobj!=undefined){
		popdetail+='<div class="popcity" style="left:'+width*(num-1)+'%"><div class="popcityscroll">'
		popdetail+='<div class="popcityoption"></div>'
		for(i in area[province]['state'][city]['city']){
			popdetail+='<div class="popcityoption'
			if((areaname!='' && areaname==area[province]['state'][city]['city'][i]['name']) || (areaname=='' && i==0)){
				areaname=area[province]['state'][city]['city'][i]['name']
				popdetail+=' act '
			}
			popdetail+='" data_key="'+i+'">'+area[province]['state'][city]['city'][i]['name']+'</div>'
		}
		popdetail+='<div class="popcityoption"></div>'
		popdetail+='</div></div>'
	}
	popdetail+='</div></div>'
	bgcover('show',0,hidepopcity)
	$('body').append(popdetail)
	var popcityoption=$('.popcityoption').height()
	$('.popcityrelativebox').append('<div style="height:2px;width:100%;background:#DCDCDC;position:absolute;top:'+(popcityoption-1)+'px"></div>')
	$('.popcityrelativebox').append('<div style="height:2px;width:100%;background:#DCDCDC;position:absolute;top:'+(popcityoption-1)*2+'px"></div>')
	$('.popcitybox').css({'display':'none','opacity':1}).slideDown(function(){
		$('.popcity').each(function(i){
			var index=$('.act',this).index()
			var set={tap:true,probeType:3}
			if(index!=-1){
				set['startY']=-popcityoption*(index-1)
				set['disablePointer']=true
			}
			var popscroll=new IScroll(this,set)
			popcityscrollinit(popscroll,i,index)
		})
	})
	$('.getpopcity').on("tap", function (event) {
		if(provinceobj!=undefined){
			provinceobj.val(provincename)
		}
		if(cityobj!=undefined){
			cityobj.val(cityname)
		}
		if(areaobj!=undefined){
			areaobj.val(areaname)
		}
		bgcover('hide')
		$('.popcitybox').slideUp(function(){
			$(this).remove()
		})
		eval(callback+'(provincename,cityname,areaname)')
		event.preventDefault();
	})

	$('.cancelpopcity').on("tap", function (event) {
		bgcover('hide')
		$('.popcitybox').slideUp(function(){
			$(this).remove()
		})
		event.preventDefault();
	})

	function popcityscrollinit(popscroll,i,index){
		var popcityoption=$('.popcityoption').height()
		popscrolllist[i]=popscroll
		simgScrollstart[i]=true
		$('.popcity').eq(i).find('.popcityoption').eq(1).addClass('act')
		popscroll.on('scroll', function(){
			if(simgScrollstart[i]==true){
				simgScrollstart[i]=false
				if(index==-1){
					index=1
				}
				popscroll.scrollTo(0,(index-1)*popcityoption,-1)
			}
		})
		popscroll.on('scrollEnd',function(){
			var acti=Math.round(this.y/popcityoption);
			if(this.y/popcityoption!=parseInt(this.y/popcityoption)){
				popscroll.scrollTo(0,(acti)*popcityoption,100)
			}
			$('.popcity').eq(i).find('.popcityoption').removeClass('act').eq(-acti+1).addClass('act')
			var changecity=false
			var changearea=false
			var value=$('.popcity').eq(i).find('.popcityoption.act').text()
			if(i==0){
				if($('.popcity').eq(0).find('.popcityoption.act').text()!=provincename){
					provincename=value
					province=$('.popcity').eq(0).find('.popcityoption.act').attr('data_key')
					changecity=true
				}
			}else if(i==1){
				if($('.popcity').eq(1).find('.popcityoption.act').text()!=cityname){
					cityname=value
					city=$('.popcity').eq(1).find('.popcityoption.act').attr('data_key')
					changearea=true
				}
			}else{
				areaname=value
			}
			if(changecity==true && cityobj!=undefined){
				var index=1
				changearea=true
				var obj=$('.popcity').eq(index)
				var html=''
				html+='<div class="popcityoption"></div>'
				for(n in area[province]['state']){
					html+='<div class="popcityoption'
					if(n==0){
						cityname=area[province]['state'][n]['name']
						city=n
						html+=' act '
					}
					html+='" data_key="'+n+'">'+area[province]['state'][n]['name']+'</div>'
				}
				html+='<div class="popcityoption"></div>'
				$('.popcityscroll').eq(index).html(html)
				//popscrolllist[2].refresh()
				popscrolllist[index].destroy()
				var set={tap:true,probeType:3,disablePointer: true}
				popscrolllist[index]=new IScroll(obj.get(0),set)
				popcityscrollinit(popscrolllist[index],index,0)
			}
			if(changearea==true && areaobj!=undefined){
				var index=2
				var obj=$('.popcity').eq(index)
				var html=''
				html+='<div class="popcityoption"></div>'
				for(n in area[province]['state'][city]['city']){
					html+='<div class="popcityoption'
					if(n==0){
						areaname=area[province]['state'][city]['city'][n]['name']
						html+=' act '
					}
					html+='" data_key="'+n+'">'+area[province]['state'][city]['city'][n]['name']+'</div>'
				}
				html+='<div class="popcityoption"></div>'
				$('.popcityscroll').eq(index).html(html)
				//popscrolllist[2].refresh()
				popscrolllist[index].destroy()
				var set={tap:true,probeType:3,disablePointer: true}
				popscrolllist[index]=new IScroll(obj.get(0),set)
				popcityscrollinit(popscrolllist[index],index,0)
			}
		});
	}
}

function hidepopcity(){
	$('.popcitybox').slideUp(function(){
		$(this).remove()
	})
}

function poptopselect(attr,value){
	attr=JSON.parse(attr)
	var popdetail='<div class="poptopselect"><div class="popselectscroll">'
	for(i in attr){
		popdetail+='<div class="poptopselectoption'
		if(value==attr[i]){
			popdetail+=' act '
		}
		popdetail+='"'
		if(i==attr.length-1){
			popdetail+=' style="border-bottom:none" '
		}
		popdetail+=' data_key="'+i+'">'+attr[i]+'</div>'
	}
	popdetail+='</div></div>'
	bgcover('show',0,hidepoptopselect)
	$('body').append(popdetail)
	$('.poptopselect').css('top',$('#header .topinfo').height()).slideDown(function(){
		new IScroll('.poptopselect',{tap:true,scrollbars: true,disablePointer: true})
	})
}

function hidepoptopselect(){
	$('.poptopselect').slideUp(function(){
		$(this).remove()
	})
}

function spcreg(text){
	var spcreg=['\\','^','$','*','+','?','{','}','.','|','!','(',')','[',']']
	for(i in spcreg){
		text=text.replace(spcreg[i],'\\'+spcreg[i])
	}
	return text
}

function getarraycn(data,flag){
	flag=parseInt(flag,2).toString(10);
	var length=0
	for(i in data){
		if(!isNaN(data[i])){
			length++
		}
	}
	length=Math.pow(2,length)-1;
	if(flag>=length){
		return false
	}else{
		flag++
		flags=flag.toString(2)
		length=data.length-flags.length
		if(length>0){
			flags=Array(length+1).join('0')+flags
		}
		return flags
	}
}
